package com.inspector.app.ui.base

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.viewbinding.ViewBinding
import com.example.commen.interf.IBaseActivity
import com.example.commen.view.WaitDialog
import com.hjq.http.listener.OnHttpListener
import com.inspector.app.di.util.autoCleared
import okhttp3.Call

abstract class BaseDJFragment<VB : ViewBinding> : Fragment(), IBaseActivity, OnHttpListener<Any> {

     var viewBinding by autoCleared<VB>()
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        viewBinding = provideViewBinding(inflater, container)
        val root: View = viewBinding.root
        setupView(root, savedInstanceState)
        return root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        initListener()
    }

    abstract fun provideViewBinding(inflater: LayoutInflater, container: ViewGroup?): VB

    protected abstract fun setupView(view: View, savedInstanceState: Bundle?)

    fun goToActivity(context: Activity, toActivity: Class<*>?) {
        val intent = Intent(context, toActivity)
        startActivity(intent)
        context.overridePendingTransition(0, 0)
    }


    override fun onSucceed(result: Any?) {

    }

    override fun onFail(e: java.lang.Exception?) {

    }

    override fun onStart(call: Call?) {
        showProgress("加载中...")
    }

    override fun onEnd(call: Call?) {
        dismissProgress()
    }
    private var dialog: WaitDialog.Builder? = null
    fun showProgress(message: String?) {
        if (requireActivity().isDestroyed) {
            return
        }
        if (dialog == null || !dialog?.isShowing()!!) {
            dialog = WaitDialog.Builder(requireActivity()).setMessage(message)
            //            dialog = new ProgressDialog(getActivity(), message);
            dialog!!.show()
        }
    }
    fun dismissProgress() {
        if (dialog?.isShowing() == true) {
            dialog!!.dismiss()
        }
    }
}
